/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nsc.utilities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.struts.util.MessageResourcesFactory;
import org.apache.struts.util.PropertyMessageResources;

/**
 *
 * @author Hue
 */
public class DBMessageResources extends PropertyMessageResources {
    private transient Connection con;
    private String modPK;
    
    public DBMessageResources(final MessageResourcesFactory factory, final String config) {
        super(factory, config);
    }
    
    public DBMessageResources(final MessageResourcesFactory factory, final String config, final boolean returnNull) {
        super(factory, config, returnNull);
    }

    public void setConnection(final Connection con) {
        this.con = con;
    }
    
    public void setModPK(final String modPK) {
        this.modPK = modPK;
    }

    public DBMessageResources newInstance() {
        return new DBMessageResources(factory, config, returnNull);
    }

    /**
     * Load the messages associated with the specified Locale key.
     * @param localeKey Locale key for the messages to be retrieved
     */ 
    @Override
    protected void loadLocale(final String localeKey) {
        // Have we already attempted to load messages for this locale?
        synchronized (locales) {
            if (locales.get(localeKey) != null) {
                return;
            }
            locales.put(localeKey, localeKey);
        }
        if(con == null) {
            return;
        }
        //the default language is fr_FR (French)
        String language = "fr_FR";
        if(localeKey != null && !"".equals(localeKey)) {
            if(localeKey.indexOf('_') == -1) {
                return;
            }
            language = localeKey;
        }
        final Properties props = new Properties();
        try {
            if(con.isClosed()) {
                return;
            }
            final String strQuery = "select TSLC_CONTEXT || '.' || TSLC_KEY, TSLV_VALUE " +
                                    "from translation_code,translation_value where TSLC_PK = TSLV_TSLC_FK and TSLC_MOD_FK = ? and TSLV_LANGUAGE = ?";
            final PreparedStatement pStmt = con.prepareStatement(strQuery);
            int iIndex = 1;
            pStmt.setString(iIndex++, modPK);
            pStmt.setString(iIndex++, language);
            final ResultSet rs = pStmt.executeQuery();
            while(rs.next()) {
                final String key = rs.getString(1);
                final String value = rs.getString("TSLV_VALUE");
                props.put(key, value);
            }
            pStmt.close();
        }
        catch(java.sql.SQLException sqle) {
            //do nothing
        }
        // Copy the corresponding values into our cache
        if (props.size() < 1) {
            return;
        }

        synchronized (messages) {
            final Enumeration names = props.keys();
            while (names.hasMoreElements()) {
                final String key = (String) names.nextElement();
                messages.put(messageKey(localeKey, key), props.getProperty(key));
            }
        }
    }
}
